package com.changpay.zywy.ui.activity.count;

import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.changpay.zywy.R;
import com.changpay.zywy.ui.dialog.SelectMonthDialog;
import com.changpay.zywy.utils.DateUtil;
import com.ruffian.library.widget.RTextView;
import com.changpay.zywy.wight.WheelCircleView;
import kale.ui.view.dialog.EasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/12/20.
 */

public class CountActionMonthView extends RelativeLayout {

    RelativeLayout rvMore;
    TextView tvTitle;
    LinearLayout llContent;


    RTextView tvTimeSearch;
    TextView tv_h9_add_count;  // H9+激活数
    TextView tv_mp70_add_count; // mp70+激活数
    TextView tv_zhibieji_count; // 自备机激活数
    TextView tv_h9_count;  // h9激活数
    TextView tv_mp70_count; // mp70激活数
    TextView tv_action_month_text; // mp70激活数
    WheelCircleView ct_action_per;  // 激活比例饼图
    WheelCircleView ct_action_per_two;  // 激活比例饼图

    android.support.v4.app.FragmentManager fragmentManager;
    CountChartActionClose countChartOtherClose;
    public boolean isDisplay = false;


    private View view;
    private DatePickerDialog.OnDateSetListener onDateSetListener;
    private String month;


    public CountActionMonthView(Context context) {
        this(context, null);
    }

    public CountActionMonthView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        month = DateUtil.INSTANCE.getYearMonth(System.currentTimeMillis());
        view = View.inflate(context, R.layout.count_item_month_action, this);
        tvTitle = view.findViewById(R.id.tv_title);
        llContent = view.findViewById(R.id.llContent);

        tv_action_month_text = findViewById(R.id.tv_action_month_text);
        tvTimeSearch = findViewById(R.id.tv_time_search);


        tv_h9_add_count = view.findViewById(R.id.tv_h9_add_count);
        tv_mp70_add_count = view.findViewById(R.id.tv_mp70_add_count);
        tv_zhibieji_count = view.findViewById(R.id.tv_zhibieji_count);
        tv_h9_count = view.findViewById(R.id.tv_h9_count);
        tv_mp70_count = view.findViewById(R.id.tv_mp70_count);
        ct_action_per = view.findViewById(R.id.ct_action_month);
        ct_action_per_two = view.findViewById(R.id.ct_action_month_two);




        tvTimeSearch.setOnClickListener(view -> {
//                DialogUtils.showDatePickerDialog(context, onDateSetListener, Calendar.getInstance());
            SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(context);
            builder.setOnDialogClickListener((v, month) -> {
                tv_action_month_text.setText(month+"激活量");
                initDateData(month);
            });
            EasyDialog build = builder.build();
            build.setCancelable(true);
            build.show(fragmentManager);
        });

        onDateSetListener = (datePicker, year, month, day) -> {
            String select_date = year+ "" + String.format("%02d", month+1);
            Log.e("mango", "onDateSet: " + select_date);
            tv_action_month_text.setText(select_date+"激活量");
            initDateData(select_date);

        };
    }

    private void initDateData(String month) {
        countChartOtherClose.loadActionMonthCharData(month);
    }

    public void setData(int h9AddAction, int mp70AddAction, int zhibeijiAction, int h9Action, int mp70Action) {
        tv_h9_add_count.setText(h9AddAction + "台");
        tv_mp70_add_count.setText(mp70AddAction + "台");
        tv_zhibieji_count.setText(zhibeijiAction + "台");
        tv_h9_count.setText(h9Action + "台");
        tv_mp70_count.setText(mp70Action + "台");
        initPerChart(ct_action_per, h9AddAction, mp70AddAction);
        initPerChart(ct_action_per_two, mp70AddAction, h9AddAction);

    }

    private void initPerChart(WheelCircleView chart, int h9AddAction, int mp70AddAction) {
       chart.setDataDan(h9AddAction,mp70AddAction);
    }



    public void setListener(CountChartActionClose listener) {
        countChartOtherClose = listener;
    }
    public void setFragmentManager(FragmentManager manager) {
        fragmentManager = manager;
    }

    public void setTitle(String title) {
        if (tvTitle != null) tvTitle.setText(title);
    }

    public interface CountChartActionClose {

        void loadActionMonthCharData(String month);
    }


}
